<?php 
namespace app\courier\controller;

use think\Controller;
use app\model\Courier;
use app\model\Registercode;
use think\Session;
use think\Cookie;

class Index extends Controller{

	// 管理员登录
	public function Index(){
		if(Cookie::has("courier_id",'backend')){
			dump(Cookie::has("courier_id","backend"));
			session::set("courier_name",Cookie::get("courier_name",'backend'));
			session::set("courier_password",Cookie::get("courier_password",'backend'));
			session::set("courier_id",Cookie::get("courier_id",'backend'));
			return $this->redirect('userdo/userlist');
		}
		return $this->fetch();
	}

	// 管理员登录
	public function Login(){

		$validate = \think\Loader::validate('Courier');
		if(!$validate->check(input('post.'))){
			$this->error($validate->getError());
		}
		$data['courier_name'] = input('courier_name');
		$data['courier_password'] = md5(input('courier_password'));
		$courier = courier::get($data);
		if(null == $courier){
			return getResponse(1,"用户名或密码错误!");
		}else{
			if(isset($_POST["rm"])){
				Cookie::prefix('backend');
				Cookie::set("courier_id",$courier['courier_id'],['prefix'=>'backend','expire'=>3600*24*30]);
				Cookie::set("courier_password",$courier['courier_password'],['prefix'=>'backend','expire'=>3600*24*30]);
				Cookie::set("courier_name",$courier['courier_name'],['prefix'=>'backend','expire'=>3600*24*30]);
			}
			session::set("courier_name",$courier['courier_name']);
			session::set("courier_password",$courier['courier_password']);
			session::set("courier_id",$courier['courier_id']);
			return $this->redirect('Orderdo/getNotdoneOrder');
		}
	}

	// 管理员登出
	public function Logout(){
		Cookie::clear("backend");
		session::clear();
		return $this->redirect("index");
	}

	public function register(){
		$code = Registercode::get(["register_code"=>input("register_code")]);
		if($code != null){
			$pars["courier_id"] = getId();
			$pars["courier_name"] = input("courier_name");
			$pars["courier_phone"] = input("courier_phone");
			$pars["courier_password"] = md5(input("courier_password"));
			Courier::create($pars);
			$code->is_use = 1;
			$code->save();
			return getResponse(0,"注册成功");
		}
		return getResponse(1,"注册码失效");
	}

	public function resetPassword(){
		$old = session::get("courier_password");
		if($old == md5(input('old'))){
			$courier = courier::get(input("courier_id"));
			$courier->courier_password = md5(input("new"));
			$courier->save();
			return getResponse(0,"修改成功");
		}
		return getResponse(1,"原密码不正确");
	}
}

 ?>